package com.atguigu.yygh.user.service.impl;/*
 * @author: XueYouPeng
 * @time: 2023/6/15 14:44
 */

import com.atguigu.yygh.feign.DictFeignClient;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.user.mapper.PatientMapper;
import com.atguigu.yygh.user.service.PatientService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {

    @Autowired
    DictFeignClient dictFeignClient;

    @Override
    public List<Patient> findAllByUserId(Long userId) {
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);

        List<Patient> patientList = baseMapper.selectList(queryWrapper);

        patientList.forEach(patient -> {
            this.packPatient(patient);//param赋值certificatesTypeString
        });
        return patientList;
    }

    @Override
    public Patient getByPatientId(Long patientId) {
        Patient patient = this.getById(patientId);
        this.packPatient(patient);
        return patient;
    }

    private void packPatient(Patient patient) {
        //在用户服务下，调用数据字典服务，根据证件类型的value查询数据字典的名称
        String name = dictFeignClient.getName(patient.getCertificatesType());

        //省市区名称
        String provinceString = dictFeignClient.getName(patient.getProvinceCode());
        String cityString = dictFeignClient.getName(patient.getCityCode());
        String districtString = dictFeignClient.getName(patient.getDistrictCode());

        patient.getParam().put("certificatesTypeString",name);
        patient.getParam().put("provinceString",provinceString);//省
        patient.getParam().put("cityString",cityString );//市
        patient.getParam().put("districtString",districtString );//区
    }
}
